<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>调拨接收编辑</title>
<!-- import css and js -->
<%@ include file="/common/metaInput.jsp"%>
<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
<script src="${ctx}/js/jsp/date.js" type="text/javascript"></script>
<script type="text/javascript">
	var basic_Url = "${ctx}/yd/logic/stockTransferEnd";
	var addressGrid;
	$(function() {
		$("#inputForm").validate({
			onsubmit : true,
			//执行ajaxsubmit  
			submitHandler : function(editform) {

				//alert(jsonInfo); 
				try {
					url = basic_Url + "/save.do";
					var api = frameElement.api;
					var W = api.opener;

					getReturnMessage(url, "inputForm", W.searchAP);
				} catch (e) {
					alert(e);
				}
			}
		}); 
		if ('${oi.billstauts}' == ""){
			$("#save1").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#save4").hide();
			$("#save5").hide();
		}else if ('${oi.billstauts}' == "0"){
			$("#save").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#save4").hide();
			$("#save5").hide();
            if($("#aegnt").val()!="${user.name}"){
    			$("#save1").hide();
    			$("input[type!='hidden']").attr("disabled",true);//禁用SELECT标签
    			$("select").attr("disabled",true);//禁用SELECT标签
    			$("textarea").attr("disabled",true);//禁用TEXTAREA标签
               }  
		}else if ('${oi.billstauts}' == "1"){
			$("#save").hide();
			$("#save1").hide();
			$("#save4").hide();
			$("input[type!='hidden']").attr("disabled",true);//禁用SELECT标签
			$("select").attr("disabled",true);//禁用SELECT标签
			$("textarea").attr("disabled",true);//禁用TEXTAREA标签
		}else if ('${oi.billstauts}' == "2"){
			$("#save").hide();
			$("#save1").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("input[type!='hidden']").attr("disabled",true);//禁用SELECT标签
			$("select").attr("disabled",true);//禁用SELECT标签
			$("textarea").attr("disabled",true);//禁用TEXTAREA标签
		}else if ('${oi.billstauts}' == "3"){
			$("#save").hide();
			$("#save1").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#save4").hide();
			$("#save5").hide();
			$("input[type!='hidden']").attr("disabled",true);//禁用SELECT标签
			$("select").attr("disabled",true);//禁用SELECT标签
			$("textarea").attr("disabled",true);//禁用TEXTAREA标签
		}else{
			$("input[type!='hidden']").attr("disabled",true);//禁用SELECT标签
			$("select").attr("disabled",true);//禁用SELECT标签
			$("textarea").attr("disabled",true);//禁用TEXTAREA标签
			$("#save").hide();
			$("#save1").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#save4").hide();
			$("#save5").hide();
		}
	});
		
	function submitForm(param) {
		var data = $("#dataGrid1").ligerGetGridManager().getData();
		if(data.length==0){
			$.dialog.alert("调拨内容不能为空");
	   	 	return false;
		}
		var ba=true;
		if(data !=null){ 
			for (var i = 0;i < data.length;i++){
				var row = data[i];
				if(parseFloat(row.quantity) > parseFloat(row.unQuantity)){
	           	 	$.dialog.alert("您第"+(i+1)+"行的本次接收入库数量大于未接收数量!!");
					ba = false;
					return false;
	            }
				if(parseFloat(row.quantity) <= 0||row.quantity==null){
		           	$.dialog.alert("您第"+(i+1)+"行的本次接收数量不合法!!");
					ba = false;
					return false;
		        }
		        if(undefined == row.bzq
						|| isNaN(row.bzq)
						|| row.bzq <= 0){
		           	 $.dialog.alert("您第"+(i+1)+"行的“保质期”不合法");
		           	 ba=false;
		           	 return false;
		        }
		        if(undefined == row.scrq || null==row.scrq||""==row.scrq){
		           	 $.dialog.alert("您第"+(i+1)+"行的“生产/采购日期”未选择");
		           	 ba=false;
		           	 return false;
		        }
		        if(undefined == row.yxq || null==row.yxq||""==row.yxq){
		           	 $.dialog.alert("您第"+(i+1)+"行的“有效期”未选择");
		           	 ba=false;
		           	 return false;
		        } 
			}
			
		}
		
		$("input[type!='hidden']").attr("disabled",false);
		$("select").attr("disabled",false);
		$("textarea").attr("disabled",false);
		//设置ajax全局同步
		jQuery.ajaxSettings.async = false;
		if(ba){
			$("#billstauts").val(param);
			$("#addressInfos").val(JSON.stringify(data));
			$("#submitFlag").val(param);
			$("#inputForm").submit();
		}
	}	
	
	function check(){
		$.ajax({
		      url: "${ctx}/yd/logic/stockTransferEnd/updateState.do",
		      global: false,
		      type: "POST",
		      dataType: "json",
		      data: ({
		    	   rows:$("#id").val()
		      }),		     
		      success: function(msg){
		    	  if(msg.msg=="SUCCESS"){
		    		  $.dialog.alert("驳回成功",function(){close();});
		    	  }
		    	  if(msg.msg=="ERROR"){
		    		  $.dialog.alert("操作失败",function(){close();});
		    	  }
		   }
		});
	}
    
	function close(){
		var api = frameElement.api;
		var W = api.opener;
		CloseDialog();
		W.searchAP();
	}
    
	/*计算两个日期之间的天数（含当天）*/
	function daysBetween(DateOne,DateTwo)
	{   
	    var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));  
	    var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);  
	    var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));  
	  
	    var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));  
	    var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);  
	    var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));  
	  
	    var days=((Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear) - Date.parse(OneMonth+'/'+OneDay+'/'+OneYear))/86400000)+1;  //包含当天
	    return days;
	}
	
</script>

</head>

<body>

	<form id="inputForm" action="" method="post">
		<input type="hidden" id="id" name="id" value="${oi.id}" />
		<input type="hidden" id="version" name="version" value="${oi.version}" />
		<input type="hidden" id="addressInfos" name="addressInfos"/>
		<input type="hidden" id="dataItem" name="dataItem"/>
		<input type="hidden" id="batchItem" name="batchItem"/>
		<input type="hidden" id="billstauts" name="billstauts" value="${oi.billstauts}"/>
		<!-- 用于标识提交按钮 -->
		<input type="hidden" id="submitFlag" value="" />
		<div class="pageContent"  id="endDiv">
			<div class="panel">
				<table class="TableBlock" align="center">
				<c:if test="${!empty oi.otherinwhcode}">
					<tr >
						<td nowrap="nowrap" width="20%">调拨接收单号:</td>
						<td width="30%"><input id="otherinwhcode" name="otherinwhcode" readonly="readonly"  class="inp_width_98p" value="${oi.otherinwhcode}" /></td>
						<td nowrap="nowrap" width="20%">建单时间:</td>
						<td width="30%"><input class="inp_width_98p" type="text" id="dctime" name="dctime"
							value="<fmt:formatDate value="${empty oi.dctime?nowDate:oi.dctime}" type="both" dateStyle="long" pattern="yyyy-MM-dd" />" readonly="readonly"/></td>
					</tr>
				</c:if>
					<tr>
						<td nowrap="nowrap" width="20%">调拨发起单号:</td> 
						<c:choose>
						<c:when test="${empty oi.oricode}">
							<td width="30%"><select id="oriC" name="oriC" class="inp_width_98p required" >
									<option ></option>
									<c:forEach items='${ori}' var='state'>
	   		                 			<option id='${state.otheroutwhcode}' value='${state.id}'>${state.otheroutwhcode}</option>
	   		                 		</c:forEach>
								</select>
								<input type="hidden" id="oricode" name="oricode" value="${oi.oricode}"/>
								<script type="text/javascript">
									$("#"+'${oi.oricode}').attr("selected","selected");
	   		                 	</script>
	   		             	</td>
	   		            </c:when>
	   		            <c:otherwise>
	   		             	<td >
								<input type="text" readonly="readonly" id="oricode" class="inp_width_98p" name="oricode" value="${oi.oricode}"/>
	   		             	</td> 
	   		            </c:otherwise>
   		             	</c:choose>
						<td nowrap="nowrap" width="20%">入库时间:</td>
						<td width="30%">
							<input id="rkrq" name="rkrq"
							class="inp_width_98p required" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})"
							value="<fmt:formatDate value="${oi.rkrq}" pattern="yyyy-MM-dd" type="both" />" />
							
							<c:if test="${oi.rkrq == null}">
								<script>
									var date = new Date();
									$("#rkrq").val(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
								</script>
							</c:if>
						</td>
					</tr>
					<tr><td nowrap="nowrap" width="20%">送货仓:</td>
						<td width="30%">
   		                 	<input type="hidden" id="outwhcode"  name="outwhcode" value="${oi.outwhcode }"/>
   		             		<input type="text" id="outwhname" class="inp_width_98p required" readonly="readonly" name="outwhname" value="${oi.outwhname }"/>
   		             	</td>
						<td nowrap="nowrap" width="20%">收货仓:</td>
						<td width="30%">
   		                 	<input type="hidden" id="whcode" name="whcode" value="${oi.whcode }"/>
   		             		<input type="text" id="whname" class="inp_width_98p required" readonly="readonly"  name="whname" value="${oi.whname }"/>
   		             	</td>
					</tr>
					<tr>
						<td nowrap="nowrap" width="20%">发起日期:</td>
						<td width="30%">
							<input id="intime" name="intime" class="inp_width_98p required" readonly="readonly"  value="<fmt:formatDate value="${oi.intime}" pattern="yyyy-MM-dd" type="both" />" />
							<c:if test="${oi.intime == null}">
								<script>
									var date = new Date();
									$("#intime").val(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
								</script>
							</c:if>
						</td>
						<td nowrap="nowrap" width="20%">制单人:</td>
						<td width="30%">
							<input type="text" id="aegnt" name="aegnt" class="inp_width_98p" value="${oi.aegnt}" readonly="readonly" />
							<c:if test="${oi.aegnt == null}">
								<script>
									$("#aegnt").val("${user.name}");
								</script>
							</c:if>
						</td>
					</tr>
					<tr>
						<td nowrap>备注:</td>
						<td colspan="3"><input id="remark" name="remark"
							class="inp_width_98p" value="${oi.remark}" />
						</td>
					</tr>
					<tr>
						<td colspan="4" style="margin-bottom: 10px;"><div
								id="dataGrid1"></div>
								</td>
					</tr>
				</table>
			</div>
			<div class="formBar">
				<ul>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm(0)">保存</button></div></div></li>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save1" onclick="submitForm(1)">提交</button></div></div></li>
				<webres:displaycontroller elementId="LOGIC_TREND_JS_2SHENHE" description="调拨接收单二级审核">
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save2" onclick="submitForm(2)" title="提交三级审核">二级审核</button></div></div></li>
		       	</webres:displaycontroller>
				<webres:displaycontroller elementId="LOGIC_TREND_JS_2WANJIE" description="调拨接收单二审完结">
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save3" onclick="submitForm(3)">二审完结</button></div></div></li>
		       	</webres:displaycontroller>
		        <webres:displaycontroller elementId="LOGIC_TREND_JS_3WANJIE" description="调拨接收单三审完结">
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save4" onclick="submitForm(3)">三审完结</button></div></div></li>
		       	</webres:displaycontroller>
		       <webres:displaycontroller elementId="LOGIC_TREND_JS_BOHUI" description="调拨接收单驳回">
		       		<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save5" onclick="check()">驳回</button></div></div></li>
		       </webres:displaycontroller>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
				</ul>
			</div>
		</div>
		<script type="text/javascript">
		var ItemStateList = ItemStateData.Rows;
    var column1 = [{ display: 'id', name: 'id',type: 'string',hide:1},
  				{ display: 'materialsid', name: 'materialsid',hide:1,width:1},
				{ display: '<font color="gray">编码</font>', name: 'code',width: 120},
				{ display: '<font color="gray">名称</font>', name: 'name',width: 120},
				{ display: '<font color="gray">型号</font>', name: 'spec' ,width: 270},
				{ display: '<font color="gray">基本单位</font>', name: 'junit' ,width: 100},
				{ display: '<font color="gray">发起基本单位数量</font>', name: 'inquantity' ,width: 150},
				{ display: '<font color="gray">接收基本单位数量</font>', name: 'jquantity',width: 150, render: function (item)
    				{
						if(item.jquantity==null||item.jquantity==""){
							return "0.0";
						}else{
							return item.jquantity;
						}
					}
				},
				{ display: '<font color="gray">批次</font>', name: 'batch', width: 120, isSort: false},
				{ display: '<font color="gray">状态</font>', name: 'status', width: 100, 
					render: function (item)
    					{
    						for (var i = 0; i < ItemStateList.length; i++)
    							{
        							if (ItemStateList[i]['ItemStateID'] == item.status){
      	  								return ItemStateList[i]['ItemStateName'];
        							}
    							}
    						return item.status;
						}
				},
				{ display: '<font color="gray">单位</font>', name: 'unit', width: 50},
   				{ display: '<font color="gray">单位换算率</font>', name: 'unitconvert',width:80},
				{ display: '<font color="gray">发起数量</font>', name: 'quantity1' ,width: 100},
				{ display: '<font color="gray">已接收入库数量</font>', name: 'hisQuantity' ,width: 120,render: function(item){
					if(item.hisQuantity==null||item.hisQuantity==""){
						return "0.0";
					}else{
						return item.hisQuantity;
					}
				}},
				{ display: '本次接收入库数量<font color="red">*</font>', name: 'quantity',width: 150,editor : {type :'float'}, render: function (item)
					{
						
   						if(item.quantity!=''&&item.quantity!=null&&item.unitconvert!=null&&item.unitconvert!=''){
   							addressGrid.updateCell('jquantity', (item.quantity*item.unitconvert).toFixed(4), item);
   						}
   						if(item.quantity==null||item.quantity==""){
   							return "0.0";
   						}else{
   							return item.quantity;
   						}
					}
				},
				{ display: '<font color="gray">未接收数量</font>', name: 'unQuantity' ,width: 100,render: function(item){
					if(item.unQuantity==null||item.unQuantity==""){
						return "0.0";
					}else{
						return item.unQuantity;
					}
				}},
				{ display: '生产/采购日期<font color="red">*</font>', name: 'scrq', width: 120,editor : {type :'scdate'},render: function (item)
					{
						if(item.scrq!=''&&item.scrq!=null){
				 		item.scrq=formatDate(new Date(item.scrq),"yyyy-MM-dd");
						}
				return item.scrq;	
					}},
			{ display: '有效期<font color="red">*</font>', name: 'yxq', width: 120,editor : {type :'scdate'}, render: function (item)
				{
				  if(item.yxq!=''&& item.yxq!=null){
					  item.yxq=formatDate(new Date(item.yxq),"yyyy-MM-dd");
				  }
					if(item.scrq!=''&&item.scrq!=null&&item.yxq!=null&&item.yxq!=''){
						addressGrid.updateCell('bzq', daysBetween(item.scrq,item.yxq), item);
					}
					if(item.yxq==''||item.yxq==null){
					return "";
				}else{
					return item.yxq;	
				}
			}},
                { display: '<font color="gray">保质期(天)</font>', name: 'bzq' ,width: 100}
				];
    var column2 = [
                   { display: 'id', name: 'id',hide:true},
                   { display: 'materialsid', name: 'materialsid',hide:true,width:1},
                   { display: '<font color="gray">编码</font>', name: 'code',width: 120},
                   { display: '<font color="gray">名称</font>', name: 'name',width: 120},
                   { display: '<font color="gray">型号</font>', name: 'spec' ,width: 270},
                   { display: '<font color="gray">基本单位</font>', name: 'junit' ,width: 100},
   				   { display: '<font color="gray">发起基本单位数量</font>', name: 'inquantity' ,width: 150},
                   { display: '<font color="gray">接收基本单位数量</font>', name: 'jquantity',width: 150},
                   { display: '<font color="gray">批次</font>', name: 'batch', width: 120},
                   { display: '<font color="gray">状态</font>', name: 'status', width: 100, render: function (item){
                       for (var i = 0; i < ItemStateList.length; i++){
                           if (ItemStateList[i]['ItemStateID'] == item.status){
                         	  return ItemStateList[i]['ItemStateName'];
                           }
                       }
                       return item.status;
                   	}
 					},
                    { display: '<font color="gray">单位</font>', name: 'unit' ,width: 100},
       				{ display: '<font color="gray">单位换算率</font>', name: 'unitconvert',width:80},
                    { display: '<font color="gray">发起数量</font>', name: 'quantity1' ,width: 100},
                    { display: '<font color="gray">已接收入库数量</font>', name: 'hisQuantity' ,width: 120},
 					{ display: '本次接收入库数量<font color="red">*</font>', name: 'quantity',width: 150 },
 					{ display: '<font color="gray">未接收数量</font>', name: 'unQuantity' ,width: 100},
 					{ display: '生产/采购日期<font color="red">*</font>', name: 'scrq' ,width: 150,render: function (item)
    					{
              			if(item.scrq!=''&&item.scrq!=null){
              	 		item.scrq=formatDate(new Date(item.scrq),"yyyy-MM-dd");
              			}
							return item.scrq;	
					}},
		               { display: '有效期至<font color="red">*</font>', name: 'yxq',width: 150, render: function (item)
    					{
	                	  if(item.yxq!=''&& item.yxq!=null){
	                		  item.yxq=formatDate(new Date(item.yxq),"yyyy-MM-dd");
	                	  }
    						return item.yxq;	
					}},
                    { display: '<font color="gray">保质期(天)</font>', name: 'bzq' ,width: 100}
	              ];
    var column3 = null;
	if ('${xx}' == 1){
		column3 = column1;
	}else {
		column3 = column2;
	}
    addressGrid = $("#dataGrid1").ligerGrid({
		 columns: column3,
	                    enabledEdit : true,
	                    usePager: true,
	      				width : '100%',
	      				pageSize:1000,
	      				pageSizeOptions:[1000],
	      				parms:[{ name : 'id' , value : '${oi.id}'}],
	      		        url : basic_Url+"/griditems.do",
	      				rownumbers : false,
	      				checkbox : true,
	                      height : '90%',
		toolbar : {
			items : [{
					text : '删除',
					click : function() {
						if ('${xx}' == 1){
							addressGrid.deleteSelectedRow();
						}
					},
					img : '${ctx}/js/ligerUI/skins/icons/delete.gif'
				}]
			}});
		
	$("#oriC").change(function(){ 
		if($("#oriC").val()==""){
			 $.dialog.alert("调拨发起单号不能为空");
			 return false;
		}
		$("#oricode").val($("#oriC").find("option:selected").text());
		<c:forEach items='${ori}' var='item'>
			if($("#oriC").find("option:selected").text()=='${item.otheroutwhcode}'){
				$("#outwhcode").val('${item.outwhcode}');
				$("#outwhname").val('${item.outwhname}');
				$("#whcode").val('${item.inwhcode}');
				$("#whname").val('${item.inwhname}');
				$("#intime").val(formatDate(new Date('${item.zkrq}'),"yyyy-MM-dd"));
			}
		</c:forEach>
		multipleSearch("#dataGrid1",true,"",basic_Url+"/stoTraItemList.do?oricode="+$("#oriC").find("option:selected").text());
	});
		<c:forEach items="${axxJson}" var="row">
		addressGrid.addRow2({
			id : "${row.id}",
			materialsid:"${row.materialsid}",
			unitconvert : "${row.unitconvert}",
	   		code : "${row.code}",
	   		name : "${row.name}",
	   		spec : "${row.spec}",
	   		junit : "${row.junit}",
	   		unit : "${row.unit}",
	   		status : "${row.status}",
	   		bzq : "${row.bzq}",
	   		scrq : new Date("${row.scrq}").formatdt('yyyy-MM-dd'),
	   		yxq : new Date("${row.yxq}").formatdt('yyyy-MM-dd'),
	   		quantity : "${row.quantity}",
	  		hisQuantity : "${row.hisQuantity}",
	  		unQuantity : "${row.unQuantity}",
	   		batch:"${row.batch}",
	   		inquantity : "${row.inquantity}",
	   		quantity1 : "${row.quantity1}",
	   		jquantity : "${row.jquantity}"
		});
	    </c:forEach>
	   /** function getHubCode(e){
	    	var rowData=addressGrid.getRow($(e).parent()[0]);
	    	var dataitem = $.parseJSON($("#dataItem").val());
	    	if(rowData.hubName == null){
	    		addressGrid.updateCell('hubName',hubname,rowData);
	    		addressGrid.updateCell('hubCode',hubcode,rowData);
	    	}else{
	    		for (var i = 0;i < dataitem.length;i++){
	    		 if (dataitem[i]['name'] == rowData.hubName){
	    			 rowData.hubCode = dataitem[i]['code'];
	    			 rowData.auto = true;
	    	   		addressGrid.updateCell('hubCode',dataitem[i]['code'],rowData);
	             }
	    		}
	    	}
	    }  **/
    </script>
	</form>
</body>
</html>
